﻿using Messhall.Application.Dto;
using Messhall.Application.Dto.Permissions.Responses;
using Messhall.Domain.Permission.IRepositorys;

namespace Messhall.Application.Permission.Services;

public class PermissionAppService
{
    private readonly IPermissionRepository _permissionRepository;

    public PermissionAppService(IPermissionRepository permissionRepository)
    {
        this._permissionRepository = permissionRepository;
    }

    /// <summary>
    /// 获取详情
    /// </summary>
    public async Task<PermissionDetailsResponse> GetDetailsAsync(RequestKeyIdBase request)
    {
        if (!request.KeyIds.Any())
        {
            throw new Exception("请传入KeyId");
        }

        var permission = await _permissionRepository.FindAsync(request.KeyIds.FirstOrDefault());

        if (permission == null)
        {
            throw new Exception("未找到节点");
        }

        var permissionCodes = await permission.GetAuthoritys();
        var authoritys = permissionCodes.Select(item => new PermissionAuthorityResponse
        {
            PermissionKeyId = item.GetPermissionKeyId(),
            Name = item.GetName(),
            Code = item.GetCode()
        }).ToList();

        var result = new PermissionDetailsResponse
        {
            KeyId = permission.GetKeyId(),
            Url = permission.GetUri(),
            Icon = permission.GetIcon(),
            ParentId = permission.GetParentId(),
            Code = permission.GetCode(),
            Remark = permission.GetRemark(),
            Name = permission.GetName(),
            Authority = authoritys
        };
        
        return result;
    }

    /// <summary>
    /// 获取树型结构
    /// </summary>
    public async Task<List<PermissionTreeResponse>> GetTreeAsync()
    {
        var permissions = await _permissionRepository.FindAllAsync();

        var result = this.PermissionRecursion(Guid.Empty, permissions);

        return result;
    }

    /// <summary>
    /// 递归构建树结构
    /// </summary>
    /// <param name="parentId"></param>
    /// <param name="permissions"></param>
    /// <returns></returns>
    private List<PermissionTreeResponse> PermissionRecursion(Guid parentId,
        List<Domain.Permission.BusinessObjects.Permission> permissions)
    {
        var permissionTreeResponse = new List<PermissionTreeResponse>();

        var parentPermissions = permissions.FindAll(permission => permission.GetParentId() == parentId);

        foreach (var item in parentPermissions)
        {
            var permission = new PermissionTreeResponse()
            {
                KeyId = item.GetKeyId(),
                ParentId = item.GetParentId(),
                Text = item.GetName(),
                Code = item.GetCode(),
                Icon = item.GetIcon(),
                Url = item.GetUri(),
                Remark = item.GetRemark()
            };

            var permissionsItems = permissions.Where(permission => permission.GetParentId() == item.GetKeyId());
            if (permissionsItems.Any())
            {
                permission.Items.AddRange(this.PermissionRecursion(item.GetKeyId(), permissions));
            }

            permissionTreeResponse.Add(permission);
        }

        return permissionTreeResponse;
    }
}